package cn.cdtu.tibetplan.service.impl;

import java.util.List;
import cn.cdtu.tibetplan.mapper.RouteMapper;
import cn.cdtu.tibetplan.pojo.PageResult;
import cn.cdtu.tibetplan.pojo.Route;
import cn.cdtu.tibetplan.pojo.RouteExample;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.cdtu.tibetplan.pojo.RouteExample.Criteria;
import cn.cdtu.tibetplan.service.RouteService;

import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class RouteServiceImpl implements RouteService {

    @Autowired
    private RouteMapper routeMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Route> findAll() {
        return routeMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Route> page = (Page<Route>) routeMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Route route) {
        routeMapper.insert(route);
    }

    /**
     * 修改
     */
    @Override
    public void update(Route route) {
        routeMapper.updateByPrimaryKeySelective(route);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Route findOne(Long id) {
        return routeMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            routeMapper.deleteByPrimaryKey(id.intValue());
        }
    }

    @Override
    public PageResult findPage(Route route, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        RouteExample example = new RouteExample();
        Criteria criteria = example.createCriteria();
        if (route != null) {
            if (route.getTitle() != null && route.getTitle().length() > 0) {
                criteria.andTitleLike("%" + route.getTitle() + "%");
            }
            if (route.getInfo() != null && route.getInfo().length() > 0) {
                criteria.andInfoLike("%" + route.getInfo() + "%");
            }
            if (route.getCreatetime() != null && route.getCreatetime().length() > 0) {
                criteria.andCreatetimeLike("%" + route.getCreatetime() + "%");
            }
            if (route.getUpdatetime() != null && route.getUpdatetime().length() > 0) {
                criteria.andUpdatetimeLike("%" + route.getUpdatetime() + "%");
            }
            if (route.getOperator() != null && route.getOperator().length() > 0) {
                criteria.andOperatorLike("%" + route.getOperator() + "%");
            }
            if (route.getOther1() != null && route.getOther1().length() > 0) {
                criteria.andOther1Like("%" + route.getOther1() + "%");
            }
            if (route.getOther2() != null && route.getOther2().length() > 0) {
                criteria.andOther2Like("%" + route.getOther2() + "%");
            }
            if (route.getOther3() != null && route.getOther3().length() > 0) {
                criteria.andOther3Like("%" + route.getOther3() + "%");
            }

        }

        Page<Route> page = (Page<Route>) routeMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
